#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : lizhen
# @Site    : 
# @File    : Shuchuang.py
# @Software: PyCharm
import re

import PyV8
import requests


class Util:
    ctx = PyV8.JSContext()
    ctx.__enter__()

    @classmethod
    def f1(cls, a):
        js = '''
            (function f1(a) {
                var encoderchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
                var b, i, len;
                var c, c2, c3;
                len = a.length;
                i = 0;
                b = "";
                while (i < len) {
                    c = a.charCodeAt(i++) & 0xff;
                    if (i == len) {
                        b += encoderchars.charAt(c >> 2);
                        b += encoderchars.charAt((c & 0x3) << 4);
                        b += "==";
                        break
                    }
                    c2 = a.charCodeAt(i++);
                    if (i == len) {
                        b += encoderchars.charAt(c >> 2);
                        b += encoderchars.charAt(((c & 0x3) << 4) | ((c2 & 0xf0) >> 4));
                        b += encoderchars.charAt((c2 & 0xf) << 2);
                        b += "=";
                        break
                    }
                    c3 = a.charCodeAt(i++);
                    b += encoderchars.charAt(c >> 2);
                    b += encoderchars.charAt(((c & 0x3) << 4) | ((c2 & 0xf0) >> 4));
                    b += encoderchars.charAt(((c2 & 0xf) << 2) | ((c3 & 0xc0) >> 6));
                    b += encoderchars.charAt(c3 & 0x3f)
                }
                return b
            })
        '''
        func = cls.ctx.eval(js)
        return func(a)


class Spider:
    def __init__(self):
        self.user_agent = {
            'User-Agent': 'User-Agent,Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                          'Chrome/69.0.3497.92 Safari/537.36'
        }
        self.url = 'http://datamining.comratings.com/exam'
        self.url2 = 'http://datamining.comratings.com/exam3'
        self.file = 'ext/exam.html'

    def save_exam_ips(self):
        with open(self.file, 'r') as f:
            html = f.read()
            exclude_strs = re.findall('\.(.*?){display:none}', html, re.S | re.M)
            exclude_strs.extend(['display:none'])
            include_strs = []
            body_list = html.split('\n')[6:]
            flag = False
            for index, item in enumerate(body_list):
                if item == '<br>':
                    include_strs.append(';')
                else:
                    for exclude_str in exclude_strs:
                        if item.find(exclude_str) != -1:
                            if flag:
                                continue
                            flag = True
                    if flag:
                        flag = False
                    else:
                        fa = re.findall(r'(<.*?>)?([.\d]+)(</.*?>)?', item, re.S)[0]
                        if fa:
                            include_strs.append(fa[1].strip())
                        else:
                            include_strs.append(item.strip().strip('</body>').strip())
            for item in ''.join(include_strs).split(';'):
                if item != '':
                    print
                    item

    def save_exam_raw(self, resp):
        with open(self.file, 'w') as f:
            f.write(resp.content.decode())

    def exec_js_exam(self):
        sess = requests.session()
        resp = sess.get(url=self.url, headers=self.user_agent)
        session = dict(resp.cookies).get('session')
        c1 = Util.f1(session[1:4])
        c2 = Util.f1(session)
        cookies = {
            'c1': c1,
            'c2': c2,
            'session': session
        }
        resp2 = sess.get(url=self.url2, headers=self.user_agent, cookies=cookies)
        self.save_exam_raw(resp2)


def main():
    s = Spider()
    s.exec_js_exam()
    s.save_exam_ips()


if __name__ == '__main__':
    main()
